package com.zjitc.service.impl;


import com.zjitc.mapper.ProductMapper;
import com.zjitc.pojo.PageInfo;
import com.zjitc.pojo.Product;
import com.zjitc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author luqi
 * @Date 2025 06 20 20 57
 **/

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public void insert(Product product) {
        productMapper.insert(product);
    }

    @Override
    public void deleteById(Integer id) {
        productMapper.deleteById(id);
    }

    @Override
    public void update(Product product) {
        productMapper.update(product);
    }

    @Override
    public Product selectById(Integer id) {
        Product product = productMapper.selectById(id);
        return product;
    }

    @Override
    public PageInfo getProductList(String keyword, Integer pageNum, Integer pageSize) {
        int total = productMapper.selectTotal(keyword);
        int pageTotal = (int) Math.ceil((double) total /pageSize);
        if (pageNum < 1) pageNum = 1;
        if (pageNum > pageTotal && pageTotal > 0) pageNum = pageTotal;
        pageNum = (pageNum - 1) * pageSize;
        List<Product> productList = productMapper.selectByKeywordPage(keyword, pageNum, pageSize);
        PageInfo pageInfo = new PageInfo(total, pageTotal, productList);
        return pageInfo;
    }
}
